import { createRouter, createWebHashHistory } from "vue-router";

import BaseLayout from "@/layouts/BaseLayout.vue";
import Home from "@/views/home/Home.vue";
import Intro from "@/views/intro/index.vue";

import TeamUp from "@/views/teamUp/index.vue";
import BuildTeam from "@/views/teamUp/BuildTeam.vue";
import TeamHub from "@/views/teamUp/TeamHub.vue";
import TeamManage from "@/views/teamUp/TeamManage.vue";
import TeamDetail from "@/views/teamUp/TeamDetail.vue";

import Innovation from "@/views/innovation/index.vue";
import All from "@/views/innovation/All.vue";
import Speciality from "@/views/innovation/Speciality.vue";
import Business from "@/views/innovation/Business.vue";
import Science from "@/views/innovation/Science.vue";
import ContestDetail from "@/views/innovation/ContestDetail.vue";
import TeacherScience from "@/views/innovation/TeacherScience.vue";

import Salon from "@/views/salon/index.vue";

import MySpace from "@/views/user/MySpace.vue";

import LoginLayout from "@/layouts/LoginLayout.vue";
import Login from "@/views/login/Login.vue";
import Forget from "@/views/login/Forget.vue";

import Forum from "@/views/salon/Forum.vue";
import KBase from "@/views/salon/KBase.vue";
import PostCreate from "@/views/salon/PostCreate.vue";
import PostView from "@/views/salon/PostView.vue";
import ArticleView from "@/views/salon/ArticleView.vue";

import Achievement from "@/views/achievement/index.vue";
import Result from "@/views/achievement/Result.vue";

import MessageCenter from "@/views/user/MessageCenter.vue";
import Reply from "@/views/user/Reply.vue";
import MyPost from "@/views/user/MyPost.vue";
import MyApply from "@/views/user/MyApply.vue";
import AppliDetail from "@/views/user/ApplyDetail.vue";
import News from "@/views/news/index.vue";
import NewsDetail from "@/views/news/NewsDetail.vue";
import { ElMessage } from "element-plus";
import { useUserStore } from "@/store";

const routes = [
  {
    path: "/",
    component: BaseLayout,
    children: [
      {
        path: "",
        component: Home,
      },
      {
        path: "intro",
        component: Intro,
      },
      //实践成果
      {
        path: "achievement",
        redirect: "/achievement/result/contest",
        meta: {
          auth: true,
        },
        children: [
          {
            path: "index",
            component: Achievement,
          },
          {
            path: "input",
            component: () => import("@/views/achievement/Input.vue"),
          },
          {
            /*
                           根据type决定成果主题

                           contest：  竞赛
                           patent：   专利
                           soft：     软著
                           paper：    论文
                           project：  项目
                       */
            path: "result/:type",
            component: Result,
            props: ({ params }) => {
              console.log(params);
              return {
                // ...params
                type: params.type,
              };
            },
          },
        ],
      },
      // 创新创业
      {
        path: "innovation",
        component: Innovation,
        children: [
          {
            path: "all",
            component: All,
          },
          {
            path: "speciality",
            component: Speciality,
          },
          {
            path: "business",
            component: Business,
          },
          {
            path: "science",
            component: Science,
          },
          {
            path: "teacher",
            component: TeacherScience,
            meta: {
              auth: true,
            },
          },
          {
            path: "detail",
            component: ContestDetail,
            // props: (route) => ({ id: Number(route.params.id) }) // 假设id是数字类型，如果不是数字类型，根据实际情况进行转换
            meta: {
              auth: true,
            },
          },
        ],
        redirect: "/innovation/all",
      },
      // 学术沙龙
      {
        path: "salon",
        component: Salon,
        redirect: "/salon/forum/idea",
        children: [
          {
            /*
                            根据type决定论坛主题

                            idea：   创意交流
                            team：   学生组队
                            market： 教师招募
                            chat：   闲聊天地
                        */
                        path: 'forum/:type',
                        name: 'forum',
                        component: Forum,
                        props: ({ params }) => {
                            // console.log(params)
                            return {
                                ...params
                            }
                        },
                    },
                    // 帖子详情页
                    {
                        path: 'forum/post/:postId',
                        name: 'post',
                        component: PostView,
                        meta: {
                            auth: true,
                        },
                        props: ({ params }) => {
                            return {
                                ...params,
                            }
                        }
                    },
                    // 发帖界面
                    {
                        path: 'create',
                        component: PostCreate,
                        meta: {
                            auth: true,
                        },
                    },
                    // 知识库
                    // {
                    //     path: 'kb',
                    //     component: KBase,
                    // },
                    // 文章详情
                    {
                        path: 'kb/:id',
                        name: 'article',
                        component: ArticleView,
                        meta: {
                            auth: true,
                        },
                        props: ({ params }) => {
                            return {
                                ...params,
                            }
                        }
                    }
                ],
            },
            // 赛事组队
            {
                path: 'teamUp',
                component: TeamUp,
                meta: {
                    auth: true,
                },
                children: [
                    {
                        path: 'teamHub',
                        component: TeamHub,
                    },
                    {
                        path: 'build',
                        component: BuildTeam,
                    },
                    {
                        path: 'manage',
                        component: TeamManage,
                    },
                    {
                        path: 'detail',
                        component: TeamDetail,
                    },
                ],
                redirect: '/teamUp/teamHub',
            },
            // 就业资讯
            // {
            //     path: 'jobInfo',
            //     component: () => import('@/views/jobInfo'),
            // },
            // 我的
            {
                path: 'user',
                component: MySpace,
                meta: {
                    auth: true,
                },
            },
            //消息中心
            {
                path: 'messages',
                component: MessageCenter,
                redirect: '/messages/reply',
                meta: {
                    auth: true,
                },
                children: [
                    {
                        path: 'reply',
                        component: Reply,
                    },
                    {
                        path: 'myPost',
                        component: MyPost,
                    },
                    {
                        path: 'myApply',
                        component: MyApply,
                    }, {
                        path: 'detail',
                        component: AppliDetail,
                    }
                ]
            },
            //新闻广播
            // {
            //     path: 'news',
            //     redirect: '/news/category/competition',
            //     children: [
            //         {
            //             path: 'category/:type',
            //             component: News,
            //         },
            //         {
            //             path: 'detail/:id',
            //             component: NewsDetail,
            //             props: true,
            //         }
            //     ]
            // }
        ]
    },
    {
        path: '/login',
        component: LoginLayout,
        children: [
            {
                path: '',
                component: Login
            },
            {
                path: 'forget',
                component: Forget
            },
        ]
    },
]

const router = createRouter({
  //Hash模式,网址后面会带/#/
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from, next) => {
  console.log("@@", to);
  const require = to.meta.auth;
  console.log(require);
  const { fetchUserInfo } = useUserStore();
  if (!require) {
    next();
  } else {
    const result = await fetchUserInfo();
    if (result) {
      next();
    } else {
      ElMessage.error("请先登录");
      next({
        path: "/login",
        params: {
          redirect: to,
        },
      });
    }
  }
});

export default router;
